Skip to content

SliceChangeIconSizeProxyModel

Base classes

Name Children Inherits
SliceIdentityProxyModel
prettyqt.itemmodels.proxies.sliceidentityproxymodel
Base model for slice proxy models.

â‹” Inheritance diagram

graph TD
  2852502553["itemmodels.SliceChangeIconSizeProxyModel"]
  4214724289["itemmodels.SliceIdentityProxyModel"]
  527136029["core.IdentityProxyModel"]
  2431222121["core.AbstractProxyModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  3668892142["QtCore.QIdentityProxyModel"]
  3989152870["QtCore.QAbstractProxyModel"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  4214724289 --> 2852502553
  527136029 --> 4214724289
  2431222121 --> 527136029
  3882849576 --> 2431222121
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  3668892142 --> 527136029
  3989152870 --> 3668892142
  1898060022 --> 3989152870
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: SliceIdentityProxyModel

Proxy model which changes the icon size of the Decoration role.

Supports QColors, QPixmaps and QIcons in DecorationRole.

Example

model = MyModel()
table = widgets.TreeView()
table.set_model(model)
table[0].proxify.change_icon_size(size=20)
table.show()
# or
proxy = itemmodels.SliceFilterProxyModel(indexer=0, size=20)
proxy.set_source_model(model)
table.set_model(proxy)
table.show()

table = widgets.TreeView()
source_model = widgets.FileSystemModel()
...
table.set_model(source_model)
# table.proxifier.change_icon_size(size=(30, 30))
Image title

table = widgets.TreeView()
source_model = widgets.FileSystemModel()
...
table.set_model(source_model)
table.proxifier.change_icon_size(size=(30, 30))
Image title

Source code in prettyqt/itemmodels/proxies/slicechangeiconsizeproxymodel.py
class SliceChangeIconSizeProxyModel(itemmodels.SliceIdentityProxyModel):
    """Proxy model which changes the icon size of the Decoration role.

    Supports QColors, QPixmaps and QIcons in DecorationRole.

    ### Example

    ```py
    model = MyModel()
    table = widgets.TreeView()
    table.set_model(model)
    table[0].proxify.change_icon_size(size=20)
    table.show()
    # or
    proxy = itemmodels.SliceFilterProxyModel(indexer=0, size=20)
    proxy.set_source_model(model)
    table.set_model(proxy)
    table.show()
    ```

    === "Without proxy"

        ```py
        table = widgets.TreeView()
        source_model = widgets.FileSystemModel()
        ...
        table.set_model(source_model)
        # table.proxifier.change_icon_size(size=(30, 30))
        ```
        <figure markdown>
          ![Image title](../../images/slicechangeiconsizeproxymodel_before.png)
        </figure>

    === "With proxy"

        ```py
        table = widgets.TreeView()
        source_model = widgets.FileSystemModel()
        ...
        table.set_model(source_model)
        table.proxifier.change_icon_size(size=(30, 30))
        ```
        <figure markdown>
          ![Image title](../../images/slicechangeiconsizeproxymodel_after.png)
        </figure>

    """

    ID = "change_icon_size"
    ICON = "mdi.resize"

    def __init__(self, size: datatypes.SizeType, **kwargs):
        super().__init__(**kwargs)
        self._size = datatypes.to_size(size)
        self._cache = {}

    def data(  # noqa: PLR0911
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if role == constants.DECORATION_ROLE and self.indexer_contains(index):
            original = super().data(index, role)
            match original:
                case gui.QIcon():
                    hashed = original.cacheKey()
                    if hashed in self._cache:
                        return self._cache[hashed]
                    p = original.pixmap(self._size)
                    self._cache[hashed] = p
                    return p
                case gui.QColor():
                    hashed = original.name()
                    if hashed in self._cache:
                        return self._cache[hashed]
                    p = gui.QPixmap(self._size)
                    p.fill(original)
                    self._cache[hashed] = p
                    return p
                case gui.QPixmap():
                    hashed = original.cacheKey()
                    if hashed in self._cache:
                        return self._cache[hashed]
                    p = original.scaled(self._size)
                    self._cache[hashed] = p
                    return p
        return super().data(index, role)

    def set_icon_size(self, size: core.QSize):
        self._cache = {}
        self._size = size

    def get_icon_size(self) -> core.QSize:
        return self._size

    icon_size = core.Property(
        core.QSize,
        get_icon_size,
        set_icon_size,
        doc="New icon size",
    )

:fa-brands-github: Show source on GitHub